package com.chocho.producerserver.controller;

import com.chocho.producerserver.bean.Test;
import com.chocho.producerserver.service.getWayService;
import com.chocho.producerserver.utils.Msg;
import com.chocho.producerserver.vo.Code;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 *公共控制层模块
 * Created by chocho on 2022/2/26.
 */
@CrossOrigin
@RestController
@RequestMapping("/common")
public class CommonController {
    @Autowired
    private getWayService getWayService;

    private Logger logger = LoggerFactory.getLogger(CommonController.class);

    @GetMapping("/hello")
    public Msg test(String hello){
        logger.info("测试接口：" + hello);
        return Msg.success();
    }

    @PreAuthorize("hasAuthority('user:compile')")
    @PostMapping("/compile")
    @ResponseBody
    public Msg compile(HttpServletRequest request, @RequestBody Code code){
        logger.info(code.getArgs());
        logger.info(code.getSource());
        try {
            getWayService.compileJava(request, code);
        } catch (Exception e) {
            return Msg.fail().add("msg", e.getMessage());
        }
        return Msg.success();
    }

    @PostMapping("/test/check")
    @ResponseBody
    public Msg checkTest(@RequestBody List<String> testIds){
        Map<String, Object> map = getWayService.checkTest(testIds);
        return "200".equals(map.get("code")) ?
                Msg.success() :
                Msg.fail().add("msg", map.get("msg")).add("detailMsg", map.get("detailMsg"));
    }

    @PostMapping("/guest/compile")
    @ResponseBody
    public Msg guestCompile(@RequestBody Code code){
        logger.info(code.getArgs());
        logger.info(code.getSource());
        try {
            getWayService.compileJavaNOSQL(code);
        } catch (Exception e) {
            return Msg.fail().add("msg", e.getMessage());
        }
        return Msg.success();
    }

}